#lang racket

;(define (count-pairs x)
;  (if (not (pair? x))
;      0
;      (+ (count-pairs (car x))
;         (count-pairs (cdr x))
;         1)))

(define (contains? seq el)
  (cond ((null? seq) false)
        ((eq? (car seq) el) true)
        (else (contains? (cdr seq) el))))

(define (count-pairs x)
  (define pairs '())
  (define (find-new-pairs x)
    (cond ((and (pair? x) (not (contains? pairs x))) (begin (set! pairs (cons x pairs))
                                                            (find-new-pairs (car x))
                                                            (find-new-pairs (cdr x))))))
  (find-new-pairs x)
  (length pairs))

(define p1 (cons 'a (cons 'b (cons 'c '()))))

(define x1 (cons 'a '()))

(define p2 (cons (cons 'b x1) x1))

(define x2 (cons 'a 'a))

(define x3 (cons x2 x2))

(define p3 (cons x3 x3))

(count-pairs p1)

(count-pairs p2)

(count-pairs p3)
